Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 618)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.02141 13.01830 13.01527 13.01230 13.00940 13.00655 13.00375 13.00099
##   [9] 12.99828 12.99560 12.99294 12.99031 12.98770 12.98510 12.98250 12.97991
##  [17] 12.97731 12.97470 12.97207 12.96943 12.96675 12.96404 12.96130 12.95851
##  [25] 12.95568 12.95278 12.94983 12.94682 12.94373 12.94058 12.93740 12.93419
##  [33] 12.93095 12.92769 12.92441 12.92112 12.91781 12.91450 12.91119 12.90788
##  [41] 12.90458 12.90130 12.89802 12.89477 12.89155 12.88835 12.88518 12.88206
##  [49] 12.87898 12.87594 12.87295 12.87002 12.86715 12.86435 12.86161 12.85894
##  [57] 12.85636 12.85385 12.85143 12.84910 12.84686 12.84472 12.84269 12.84076
##  [65] 12.83883 12.83679 12.83463 12.83237 12.83001 12.82756 12.82502 12.82241
##  [73] 12.81972 12.81696 12.81415 12.81127 12.80835 12.80538 12.80238 12.79934
##  [81] 12.79628 12.79320 12.79011 12.78701 12.78391 12.78081 12.77772 12.77465
##  [89] 12.77161 12.76859 12.76560 12.76266 12.75977 12.75692 12.75414 12.75142
##  [97] 12.74877 12.74620 12.74371 12.74132 12.73901 12.73681 12.73471 12.73273
## [105] 12.73087 12.72913 12.72752 12.72605 12.72473 12.72355 12.72253 12.72167
## [113] 12.72098 12.72046 12.72012 12.71996 12.72000 12.72023 12.72067 12.72132
## [121] 12.72218 12.72326 12.72458 12.72612 12.72791 12.72994 12.73222 12.73476
## [129] 12.73831 12.74350 12.75016 12.75811 12.76719 12.77722 12.78802 12.79943
## [137] 12.81127 12.82337 12.83556 12.84766 12.85950 12.87091 12.88172 12.89175
## [145] 12.90083 12.90879 12.91545 12.92308 12.93377 12.94709 12.96260 12.97989
## [153] 12.99851 13.01804 13.03806 13.05813 13.07781 13.09670 13.11434 13.13032
## [161] 13.14420 13.15556 13.16397 13.17103 13.17863 13.18670 13.19521 13.20410
## [169] 13.21334 13.22286 13.23263 13.24260 13.25272 13.26294 13.27321 13.28349
## [177] 13.29374 13.30389 13.31392 13.32376 13.33337 13.34270 13.35172 13.36036
## [185] 13.36858 13.37634 13.38359 13.39027 13.39635 13.40177 13.40650 13.41047
## [193] 13.41365 13.41598 13.41742 13.41792 13.41744 13.41592 13.41332 13.40960
## [201] 13.40470 13.39857 13.39118 13.38247 13.37240 13.36001 13.34459 13.32645
## [209] 13.30589 13.28321 13.25871 13.23270 13.20548 13.17735 13.14861 13.11958
## [217] 13.09055 13.06182 13.03369 13.00648 12.98048 12.95600 12.93334 12.91279
## [225] 12.89088 12.86435 12.83384 12.80000 12.76349 12.72496 12.68506 12.64443
## [233] 12.60374 12.56363 12.52475 12.48775 12.45329 12.42201 12.39457 12.37162
## [241] 12.35142 12.33180 12.31275 12.29426 12.27631 12.25888 12.24197 12.22556
## [249] 12.20964 12.19419 12.17920 12.16465 12.15054 12.13685 12.12356 12.11066
## [257] 12.09814 12.08599 12.07418 12.06271 12.05157 12.04074 12.03020 12.01994
## [265] 12.00996 12.00023 11.99074 11.98148 11.97243 11.96359 11.95493 11.94645
## [273] 11.93812 11.93107 11.92621 11.92323 11.92183 11.92171 11.92256 11.92407
## [281] 11.92595 11.92788 11.92956 11.93070 11.93097 11.93009 11.92773 11.92361
## [289] 11.91742 11.91032 11.90365 11.89735 11.89137 11.88565 11.88012 11.87473
## [297] 11.86941 11.86412 11.85879 11.85335 11.84776 11.84195 11.83587 11.82945
## [305] 11.82264 11.81537 11.80760 11.79925 11.78959 11.77800 11.76465 11.74968
## [313] 11.73325 11.71551 11.69661 11.67670 11.65594 11.63447 11.61245 11.59004
## [321] 11.56738 11.54462 11.52192 11.49944 11.47731 11.45570 11.43476 11.41464
## [329] 11.39548 11.37746 11.36070 11.34538 11.33163 11.31961 11.30948 11.30139
## [337] 11.29548 11.29192 11.28947 11.28689 11.28426 11.28169 11.27928 11.27712
## [345] 11.27532 11.27398 11.27319 11.27305 11.27366 11.27513 11.27755 11.28101
## [353] 11.28563 11.29149 11.29870 11.30736 11.31757 11.33029 11.34626 11.36519
## [361] 11.38679 11.41077 11.43687 11.46478 11.49423 11.52493 11.55659 11.58894
## [369] 11.62168 11.65454 11.68722 11.71945 11.75093 11.78139 11.81054 11.83809
## [377] 11.86377 11.88728 11.91086 11.93680 11.96492 11.99503 12.02693 12.06045
## [385] 12.09538 12.13155 12.16876 12.20682 12.24554 12.28474 12.32423 12.36381
## [393] 12.40330 12.44251 12.48125 12.51933 12.55656 12.59275 12.62772 12.66127
## [401] 12.69322 12.72337 12.75154 12.77754 12.80118 12.82226 12.84061 12.85603
## [409] 12.86949 12.88209 12.89385 12.90478 12.91490 12.92422 12.93276 12.94054
## [417] 12.94757 12.95387 12.95945 12.96433 12.96852 12.97205 12.97492 12.97716
## [425] 12.97877 12.97978 12.98020 12.97833 12.97277 12.96398 12.95242 12.93855
## [433] 12.92283 12.90572 12.88768 12.86917 12.85064 12.83257 12.81540 12.79960
## [441] 12.78563 12.77394 12.76500 12.75679 12.74705 12.73587 12.72336 12.70961
## [449] 12.69473 12.67882 12.66198 12.64430 12.62589 12.60685 12.58728 12.56728
## [457] 12.54694 12.52637 12.50568 12.48495 12.46428 12.44379 12.42357 12.40372
## [465] 12.38434 12.36553 12.34738 12.33001 12.31351 12.29798 12.28352 12.27023
## [473] 12.25821 12.24757 12.23839 12.23079 12.22369 12.21603 12.20796 12.19962
## [481] 12.19114 12.18267 12.17434 12.16629 12.15866 12.15160 12.14523 12.13969
## [489] 12.13514 12.13169 12.12950 12.12871 12.12950 12.13189 12.13574 12.14090
## [497] 12.14723 12.15458 12.16280 12.17176 12.18131 12.19131 12.20161 12.21206
## [505] 12.22253 12.23286 12.24292 12.25256 12.26163 12.27000 12.27751 12.28520
## [513] 12.29413 12.30424 12.31544 12.32765 12.34080 12.35480 12.36959 12.38508
## [521] 12.40120 12.41786 12.43499 12.45251 12.47035 12.48842 12.50664 12.52495
## [529] 12.54326 12.56149 12.57957 12.59741 12.61494 12.63209 12.64877 12.66491
## [537] 12.68042 12.69523 12.70927 12.72245 12.73470 12.74593 12.75608 12.76506
## [545] 12.77279 12.77920 12.78421 12.78774 12.78972 12.79006 12.78868 12.78552
## [553] 12.78048 12.77439 12.76806 12.76144 12.75448 12.74711 12.73927 12.73092
## [561] 12.72200 12.71244 12.70219 12.69120 12.67941 12.66675 12.65319 12.63865
## [569] 12.62308 12.60678 12.59008 12.57293 12.55531 12.53718 12.51852 12.49929
## [577] 12.47947 12.45902 12.43790 12.41610 12.39358 12.37030 12.34624 12.32136
## [585] 12.29564 12.26904 12.24153 12.21309 12.18378 12.15372 12.12290 12.09133
## [593] 12.05901 12.02594 11.99213 11.95757 11.92227 11.88624 11.84946 11.81195
## [601] 11.77371 11.73474 11.69503 11.65461 11.61345 11.57158 11.52899 11.48568
## [609] 11.44165 11.39691 11.35147 11.30531 11.25844 11.21088 11.16261 11.11364
## [617] 11.06397 11.01360
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 618)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.62518 12.62069 12.61631 12.61204 12.60787 12.60381 12.59984 12.59597
##   [9] 12.59219 12.58850 12.58489 12.58136 12.57791 12.57453 12.57122 12.56798
##  [17] 12.56480 12.56168 12.55861 12.55560 12.55263 12.54971 12.54683 12.54399
##  [25] 12.54119 12.53841 12.53567 12.53294 12.53024 12.52755 12.52489 12.52226
##  [33] 12.51965 12.51708 12.51455 12.51206 12.50961 12.50721 12.50487 12.50258
##  [41] 12.50035 12.49819 12.49610 12.49408 12.49213 12.49026 12.48848 12.48678
##  [49] 12.48518 12.48367 12.48225 12.48094 12.47974 12.47865 12.47767 12.47680
##  [57] 12.47606 12.47544 12.47496 12.47460 12.47438 12.47430 12.47437 12.47458
##  [65] 12.47491 12.47532 12.47580 12.47637 12.47703 12.47777 12.47859 12.47951
##  [73] 12.48052 12.48162 12.48281 12.48411 12.48550 12.48699 12.48858 12.49027
##  [81] 12.49207 12.49397 12.49599 12.49811 12.50035 12.50270 12.50516 12.50775
##  [89] 12.51045 12.51327 12.51621 12.51928 12.52247 12.52579 12.52924 12.53282
##  [97] 12.53654 12.54039 12.54437 12.54845 12.55260 12.55681 12.56108 12.56541
## [105] 12.56980 12.57424 12.57875 12.58331 12.58792 12.59259 12.59732 12.60210
## [113] 12.60693 12.61181 12.61674 12.62173 12.62676 12.63184 12.63697 12.64214
## [121] 12.64736 12.65263 12.65794 12.66329 12.66868 12.67412 12.67960 12.68512
## [129] 12.69122 12.69840 12.70652 12.71548 12.72515 12.73542 12.74618 12.75731
## [137] 12.76869 12.78021 12.79175 12.80320 12.81443 12.82534 12.83580 12.84570
## [145] 12.85493 12.86337 12.87090 12.87994 12.89264 12.90849 12.92697 12.94757
## [153] 12.96978 12.99308 13.01696 13.04090 13.06440 13.08694 13.10800 13.12707
## [161] 13.14364 13.15719 13.16721 13.17539 13.18374 13.19221 13.20079 13.20942
## [169] 13.21809 13.22675 13.23536 13.24391 13.25234 13.26063 13.26874 13.27664
## [177] 13.28429 13.29166 13.29872 13.30542 13.31174 13.31764 13.32309 13.32806
## [185] 13.33250 13.33638 13.33967 13.34234 13.34435 13.34567 13.34626 13.34609
## [193] 13.34512 13.34332 13.34065 13.33709 13.33259 13.32712 13.32065 13.31315
## [201] 13.30457 13.29489 13.28406 13.27206 13.25886 13.24289 13.22292 13.19938
## [209] 13.17273 13.14340 13.11182 13.07845 13.04371 13.00806 12.97193 12.93576
## [217] 12.89999 12.86506 12.83142 12.79949 12.76974 12.74258 12.71847 12.69784
## [225] 12.67714 12.65286 12.62555 12.59572 12.56390 12.53063 12.49644 12.46185
## [233] 12.42739 12.39360 12.36100 12.33013 12.30150 12.27566 12.25313 12.23444
## [241] 12.21840 12.20344 12.18952 12.17660 12.16462 12.15356 12.14335 12.13397
## [249] 12.12536 12.11749 12.11031 12.10377 12.09784 12.09247 12.08761 12.08322
## [257] 12.07927 12.07570 12.07247 12.06954 12.06687 12.06440 12.06211 12.05994
## [265] 12.05785 12.05580 12.05375 12.05164 12.04944 12.04711 12.04460 12.04186
## [273] 12.03886 12.03769 12.04015 12.04570 12.05381 12.06397 12.07564 12.08830
## [281] 12.10142 12.11447 12.12694 12.13828 12.14797 12.15550 12.16032 12.16192
## [289] 12.15976 12.15537 12.15063 12.14554 12.14012 12.13438 12.12834 12.12201
## [297] 12.11540 12.10853 12.10141 12.09405 12.08646 12.07867 12.07067 12.06249
## [305] 12.05414 12.04563 12.03698 12.02819 12.01810 12.00564 11.99099 11.97432
## [313] 11.95583 11.93567 11.91405 11.89112 11.86708 11.84210 11.81636 11.79004
## [321] 11.76331 11.73636 11.70937 11.68251 11.65596 11.62991 11.60452 11.57998
## [329] 11.55647 11.53417 11.51325 11.49390 11.47629 11.46060 11.44701 11.43570
## [337] 11.42685 11.42064 11.41566 11.41045 11.40513 11.39981 11.39460 11.38961
## [345] 11.38494 11.38073 11.37706 11.37406 11.37183 11.37049 11.37014 11.37090
## [353] 11.37288 11.37619 11.38094 11.38724 11.39520 11.40575 11.41952 11.43625
## [361] 11.45564 11.47740 11.50127 11.52693 11.55412 11.58255 11.61192 11.64197
## [369] 11.67239 11.70290 11.73323 11.76308 11.79217 11.82021 11.84692 11.87201
## [377] 11.89519 11.91619 11.93705 11.95990 11.98460 12.01098 12.03889 12.06817
## [385] 12.09867 12.13024 12.16271 12.19594 12.22976 12.26403 12.29858 12.33326
## [393] 12.36791 12.40239 12.43653 12.47017 12.50317 12.53537 12.56661 12.59673
## [401] 12.62559 12.65302 12.67887 12.70299 12.72521 12.74539 12.76337 12.77899
## [409] 12.79362 12.80868 12.82403 12.83955 12.85512 12.87061 12.88590 12.90087
## [417] 12.91539 12.92933 12.94258 12.95500 12.96649 12.97690 12.98613 12.99403
## [425] 13.00050 13.00541 13.00863 13.00955 13.00783 13.00375 12.99760 12.98965
## [433] 12.98021 12.96953 12.95792 12.94564 12.93299 12.92025 12.90770 12.89563
## [441] 12.88431 12.87403 12.86507 12.85575 12.84426 12.83077 12.81539 12.79829
## [449] 12.77959 12.75944 12.73799 12.71536 12.69170 12.66716 12.64187 12.61598
## [457] 12.58963 12.56295 12.53609 12.50919 12.48239 12.45584 12.42966 12.40402
## [465] 12.37903 12.35486 12.33163 12.30949 12.28858 12.26904 12.25101 12.23463
## [473] 12.22005 12.20741 12.19684 12.18849 12.18053 12.17124 12.16086 12.14967
## [481] 12.13793 12.12588 12.11381 12.10196 12.09061 12.08001 12.07042 12.06210
## [489] 12.05532 12.05035 12.04743 12.04683 12.04826 12.05118 12.05545 12.06098
## [497] 12.06762 12.07528 12.08383 12.09314 12.10311 12.11362 12.12454 12.13576
## [505] 12.14716 12.15862 12.17002 12.18124 12.19217 12.20269 12.21268 12.22336
## [513] 12.23595 12.25034 12.26643 12.28411 12.30326 12.32377 12.34555 12.36847
## [521] 12.39243 12.41732 12.44303 12.46944 12.49646 12.52397 12.55186 12.58002
## [529] 12.60834 12.63671 12.66503 12.69319 12.72106 12.74855 12.77555 12.80194
## [537] 12.82762 12.85248 12.87640 12.89928 12.92101 12.94148 12.96058 12.97820
## [545] 12.99423 13.00856 13.02108 13.03169 13.04026 13.04670 13.05090 13.05274
## [553] 13.05211 13.04992 13.04715 13.04377 13.03976 13.03511 13.02979 13.02380
## [561] 13.01710 13.00969 13.00154 12.99264 12.98296 12.97250 12.96122 12.94912
## [569] 12.93617 12.92252 12.90830 12.89350 12.87809 12.86206 12.84538 12.82803
## [577] 12.80999 12.79125 12.77177 12.75155 12.73055 12.70876 12.68615 12.66272
## [585] 12.63843 12.61326 12.58719 12.56021 12.53237 12.50373 12.47430 12.44408
## [593] 12.41307 12.38128 12.34869 12.31532 12.28116 12.24622 12.21049 12.17398
## [601] 12.13670 12.09863 12.05978 12.02016 11.97976 11.93859 11.89664 11.85392
## [609] 11.81043 11.76617 11.72113 11.67534 11.62877 11.58144 11.53334 11.48448
## [617] 11.43486 11.38448
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 618)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.01976 12.01504 12.01043 12.00594 12.00156 11.99727 11.99308 11.98898
##   [9] 11.98496 11.98101 11.97713 11.97332 11.96957 11.96586 11.96220 11.95859
##  [17] 11.95500 11.95145 11.94791 11.94439 11.94089 11.93738 11.93388 11.93036
##  [25] 11.92683 11.92328 11.91971 11.91610 11.91245 11.90877 11.90503 11.90123
##  [33] 11.89737 11.89345 11.88945 11.88537 11.88124 11.87707 11.87290 11.86870
##  [41] 11.86451 11.86031 11.85612 11.85194 11.84779 11.84366 11.83956 11.83550
##  [49] 11.83148 11.82752 11.82361 11.81977 11.81600 11.81231 11.80870 11.80517
##  [57] 11.80175 11.79842 11.79521 11.79211 11.78913 11.78628 11.78356 11.78098
##  [65] 11.77855 11.77628 11.77416 11.77221 11.77043 11.76883 11.76741 11.76603
##  [73] 11.76453 11.76293 11.76124 11.75946 11.75762 11.75571 11.75374 11.75174
##  [81] 11.74970 11.74765 11.74558 11.74351 11.74145 11.73941 11.73740 11.73543
##  [89] 11.73351 11.73165 11.72986 11.72816 11.72654 11.72503 11.72363 11.72235
##  [97] 11.72120 11.72020 11.71934 11.71866 11.71814 11.71781 11.71767 11.71774
## [105] 11.71802 11.71853 11.71927 11.72025 11.72150 11.72300 11.72479 11.72686
## [113] 11.72922 11.73189 11.73488 11.73820 11.74185 11.74586 11.75022 11.75494
## [121] 11.76005 11.76555 11.77144 11.77774 11.78447 11.79162 11.79921 11.80725
## [129] 11.81691 11.82915 11.84371 11.86032 11.87871 11.89860 11.91972 11.94182
## [137] 11.96461 11.98782 12.01119 12.03444 12.05731 12.07952 12.10080 12.12088
## [145] 12.13950 12.15638 12.17126 12.18740 12.20787 12.23207 12.25938 12.28920
## [153] 12.32092 12.35394 12.38766 12.42145 12.45473 12.48688 12.51730 12.54537
## [161] 12.57050 12.59208 12.60950 12.62480 12.64039 12.65623 12.67226 12.68844
## [169] 12.70472 12.72106 12.73741 12.75372 12.76994 12.78603 12.80194 12.81762
## [177] 12.83303 12.84811 12.86283 12.87713 12.89097 12.90429 12.91706 12.92923
## [185] 12.94074 12.95156 12.96163 12.97091 12.97934 12.98689 12.99351 12.99914
## [193] 13.00375 13.00728 13.00969 13.01092 13.01095 13.00970 13.00715 13.00323
## [201] 12.99792 12.99115 12.98287 12.97306 12.96165 12.94685 12.92726 12.90339
## [209] 12.87577 12.84491 12.81133 12.77555 12.73809 12.69947 12.66020 12.62082
## [217] 12.58183 12.54376 12.50712 12.47243 12.44022 12.41100 12.38530 12.36362
## [225] 12.34195 12.31630 12.28724 12.25536 12.22123 12.18542 12.14852 12.11109
## [233] 12.07373 12.03700 12.00148 11.96775 11.93639 11.90797 11.88307 11.86227
## [241] 11.84409 11.82665 11.80993 11.79390 11.77853 11.76380 11.74967 11.73612
## [249] 11.72313 11.71067 11.69871 11.68721 11.67617 11.66554 11.65531 11.64544
## [257] 11.63590 11.62668 11.61774 11.60905 11.60060 11.59235 11.58427 11.57634
## [265] 11.56853 11.56081 11.55317 11.54556 11.53796 11.53035 11.52270 11.51497
## [273] 11.50716 11.50110 11.49835 11.49845 11.50094 11.50536 11.51124 11.51813
## [281] 11.52555 11.53305 11.54017 11.54643 11.55139 11.55457 11.55551 11.55376
## [289] 11.54884 11.54191 11.53446 11.52655 11.51823 11.50955 11.50059 11.49138
## [297] 11.48200 11.47249 11.46291 11.45332 11.44378 11.43434 11.42505 11.41599
## [305] 11.40719 11.39873 11.39064 11.38301 11.37453 11.36401 11.35160 11.33749
## [313] 11.32182 11.30476 11.28649 11.26715 11.24693 11.22597 11.20445 11.18254
## [321] 11.16039 11.13817 11.11604 11.09418 11.07274 11.05188 11.03179 11.01261
## [329] 10.99451 10.97766 10.96222 10.94836 10.93624 10.92602 10.91788 10.91197
## [337] 10.90846 10.90752 10.90876 10.91165 10.91605 10.92187 10.92898 10.93728
## [345] 10.94665 10.95697 10.96814 10.98004 10.99256 11.00558 11.01900 11.03269
## [353] 11.04656 11.06047 11.07432 11.08800 11.10140 11.11607 11.13349 11.15339
## [361] 11.17554 11.19968 11.22555 11.25290 11.28148 11.31104 11.34132 11.37207
## [369] 11.40304 11.43397 11.46462 11.49473 11.52404 11.55231 11.57928 11.60469
## [377] 11.62831 11.64986 11.67098 11.69338 11.71695 11.74157 11.76714 11.79355
## [385] 11.82069 11.84844 11.87671 11.90536 11.93431 11.96343 11.99262 12.02176
## [393] 12.05075 12.07948 12.10783 12.13570 12.16297 12.18954 12.21529 12.24012
## [401] 12.26391 12.28656 12.30795 12.32798 12.34652 12.36349 12.37875 12.39221
## [409] 12.40478 12.41741 12.43002 12.44256 12.45494 12.46712 12.47901 12.49055
## [417] 12.50168 12.51232 12.52241 12.53188 12.54066 12.54868 12.55589 12.56220
## [425] 12.56756 12.57189 12.57513 12.57644 12.57520 12.57172 12.56630 12.55923
## [433] 12.55081 12.54135 12.53114 12.52048 12.50966 12.49899 12.48877 12.47929
## [441] 12.47085 12.46375 12.45828 12.45306 12.44653 12.43878 12.42988 12.41991
## [449] 12.40895 12.39707 12.38436 12.37089 12.35674 12.34199 12.32672 12.31100
## [457] 12.29492 12.27855 12.26196 12.24525 12.22849 12.21174 12.19510 12.17865
## [465] 12.16245 12.14659 12.13114 12.11619 12.10181 12.08808 12.07508 12.06289
## [473] 12.05158 12.04123 12.03192 12.02374 12.01522 12.00505 11.99351 11.98089
## [481] 11.96747 11.95354 11.93937 11.92524 11.91145 11.89828 11.88600 11.87490
## [489] 11.86527 11.85738 11.85152 11.84798 11.84584 11.84402 11.84253 11.84139
## [497] 11.84062 11.84023 11.84023 11.84064 11.84148 11.84276 11.84449 11.84669
## [505] 11.84937 11.85256 11.85626 11.86049 11.86527 11.87060 11.87652 11.88351
## [513] 11.89201 11.90196 11.91325 11.92582 11.93957 11.95443 11.97031 11.98713
## [521] 12.00481 12.02326 12.04241 12.06216 12.08245 12.10317 12.12426 12.14563
## [529] 12.16720 12.18888 12.21060 12.23226 12.25379 12.27511 12.29613 12.31677
## [537] 12.33694 12.35657 12.37558 12.39387 12.41137 12.42800 12.44366 12.45829
## [545] 12.47179 12.48409 12.49511 12.50475 12.51294 12.51959 12.52463 12.52796
## [553] 12.52951 12.53012 12.53064 12.53100 12.53117 12.53108 12.53068 12.52991
## [561] 12.52873 12.52706 12.52486 12.52208 12.51865 12.51453 12.50966 12.50397
## [569] 12.49743 12.49035 12.48306 12.47553 12.46772 12.45959 12.45111 12.44224
## [577] 12.43293 12.42317 12.41289 12.40208 12.39068 12.37867 12.36601 12.35266
## [585] 12.33857 12.32373 12.30807 12.29158 12.27434 12.25648 12.23798 12.21887
## [593] 12.19913 12.17877 12.15780 12.13621 12.11401 12.09120 12.06779 12.04377
## [601] 12.01915 11.99393 11.96811 11.94170 11.91470 11.88711 11.85893 11.83017
## [609] 11.80083 11.77090 11.74040 11.70933 11.67768 11.64546 11.61268 11.57933
## [617] 11.54541 11.51094
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")